package com.lechuang.goods.manager.impl;

import cn.hutool.core.bean.BeanUtil;
import com.lechuang.common.model.BaseEntity;
import com.lechuang.goods.dto.GoodsCategoryListDTO;
import com.lechuang.goods.dto.GoodsCategorySaveDTO;
import com.lechuang.goods.entity.GoodsCategory;
import com.lechuang.goods.manager.GoodsCategoryManager;
import com.lechuang.goods.service.GoodsCategoryService;
import com.lechuang.goods.vo.GoodsCategoryDetailVO;
import com.lechuang.goods.vo.GoodsCategoryListVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class GoodsCategoryManagerImpl implements GoodsCategoryManager {

    @Resource
    private GoodsCategoryService goodsCategoryService;

    @Override
    public void saveOrUpdate(GoodsCategorySaveDTO goodsCategorySaveDTO) {
        Long id = goodsCategorySaveDTO.getId();
        if(id == null){
            this.save(goodsCategorySaveDTO);
        }else{
            this.update(goodsCategorySaveDTO);
        }

    }

    @Override
    public List<GoodsCategoryListVO> list(GoodsCategoryListDTO goodsCategoryListDTO) {
        List<GoodsCategory> categoryList = goodsCategoryService.list(goodsCategoryListDTO);
        List<GoodsCategory> firstCategory = categoryList.stream().filter(a -> a.getLevel() == 1).toList();
        List<GoodsCategoryListVO> voList = new ArrayList<>();
        firstCategory.forEach(bean -> {
            List<GoodsCategory> childList = categoryList.stream().filter(a -> Objects.equals(a.getParentId(), bean.getId())).toList();
            GoodsCategoryListVO vo = BeanUtil.copyProperties(bean, GoodsCategoryListVO.class);
            List<GoodsCategoryListVO> childVoList = new ArrayList<>();
            childList.forEach(childBean -> {
                GoodsCategoryListVO childVo = BeanUtil.copyProperties(childBean, GoodsCategoryListVO.class);
                childVoList.add(childVo);
            });
            vo.setChild(childVoList);
            voList.add(vo);
        });
        return voList;
    }

    @Override
    public void delete(Long id) {
        List<GoodsCategory> goodsCategoryList = goodsCategoryService.getByParentId(id);
        List<Long> idList = goodsCategoryList.stream().map(BaseEntity::getId).collect(Collectors.toList());
        idList.add(id);
        goodsCategoryService.removeBatchByIds(idList);
    }

    @Override
    public GoodsCategoryDetailVO detail(Long id) {
        GoodsCategory goodsCategory = goodsCategoryService.getById(id);
        return BeanUtil.copyProperties(goodsCategory, GoodsCategoryDetailVO.class);
    }

    private void update(GoodsCategorySaveDTO goodsCategorySaveDTO) {
        GoodsCategory goodsCategory = new GoodsCategory()
                .setName(goodsCategorySaveDTO.getName())
                .setLevel(goodsCategorySaveDTO.getLevel())
                .setParentId(goodsCategorySaveDTO.getParentId())
                .setSort(goodsCategorySaveDTO.getSort());
        goodsCategory.setId(goodsCategorySaveDTO.getId());
        goodsCategoryService.updateById(goodsCategory);
    }

    private void save(GoodsCategorySaveDTO goodsCategorySaveDTO) {
        GoodsCategory goodsCategory = new GoodsCategory()
                .setName(goodsCategorySaveDTO.getName())
                .setLevel(goodsCategorySaveDTO.getLevel())
                .setParentId(goodsCategorySaveDTO.getParentId())
                .setSort(goodsCategorySaveDTO.getSort());
        goodsCategoryService.save(goodsCategory);
    }
}
